package com.A1908.servlet;

import com.A1908.dao.impl.DataAnalysisDaoImpl;
import com.A1908.entity.TotalDataAnalysis;
import com.A1908.entity.TreeDataAnalysis;
import com.A1908.entity.User;
import com.A1908.entity.UserFeastType;
import com.A1908.service.UserFeastTypeService;
import com.A1908.service.impl.DataAnalysisServiceImpl;
import com.A1908.service.impl.UserFeastTypeServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/feastType"})
public class UserFeastTypeServlet extends HttpServlet {
    UserFeastTypeService feastTypeService = new UserFeastTypeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String opr = request.getParameter("opr");
        PrintWriter out = response.getWriter();

        if ("list".equals(opr) || opr == null) {
            List<UserFeastType> list = feastTypeService.findAllFeastType();
            out.print(JSON.toJSONString(list));
        } else if ("getFeastInfo".equals(opr)) {
            User user = new User();
            user.setId((int) request.getSession().getAttribute("userID"));

            DataAnalysisServiceImpl data = new DataAnalysisServiceImpl();
            TotalDataAnalysis totalData = data.totalData(user);
            if (data != null) {
                out.print(JSON.toJSONString(totalData));
            } else {
                out.print("false");
            }
        }

        out.close();
    }
}
